﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using JinRi.Services;
using JinRi.Services.Domains;
using JinRi.WebHost.ViewModels;

namespace JinRi.WebHost.Controllers
{
    public class HomeController : Controller
    {
        protected readonly DataService Service = new DataService();
        public ActionResult Index()
        {
            var banner = Service.GetBanner(3);
            var model = Mapper.Map<Banner, BannerModel>(banner);
            return View(model);
        }

        public ActionResult About()
        {
            var entity = Service.GetAbout();
            var model = Mapper.Map<About, AboutModel>(entity);

            var awards = Service.FindAll<Award>().OrderBy(p => p.CreateTime).ToList();
            ViewBag.Awards = Mapper.Map<List<Award>, List<AwardModel>>(awards);
            return View(model);
        }

        public ActionResult Product()
        {
            var entity = Service.FindAll<Product>().OrderBy(p => p.CreateTime).ToList();
            var products = Mapper.Map<List<Product>, List<ProductModel>>(entity);
            ViewBag.Products = products;
            return View();
        }

        public ActionResult Video()
        {
            var entity = Service.FindAll<Video>().OrderBy(p => p.CreateTime).ToList();
            var videos = Mapper.Map<List<Video>, List<VideoModel>>(entity);
            ViewBag.Videos = videos;
            return View();
        }

        public ActionResult Derivative()
        {
            return View();
        }

        public ActionResult News()
        {
            var banner = Service.GetBanner(1);
            var items = Service.FindAll<News>().OrderByDescending(p => p.CreateTime).ToList();
            ViewBag.News = Mapper.Map<List<News>, List<NewsModel>>(items.Where(p => p.Category == 0).ToList());
            ViewBag.Notices = Mapper.Map<List<News>, List<NewsModel>>(items.Where(p => p.Category == 1).ToList());
            return View(banner == null ? Guid.Empty : banner.ImageId);
        }

        public ActionResult Job()
        {
            var banner = Service.GetBanner(2);
            var items = Service.FindAll<Job>().OrderBy(p => p.CreateTime).ToList();
            ViewBag.Jobs = Mapper.Map<List<Job>, List<JobModel>>(items);
            var model = Mapper.Map<Banner, BannerModel>(banner);
            return View(model);
        }

        public ActionResult Contact()
        {
            var entity = Service.GetContact();
            var model = Mapper.Map<Contact, ContactModel>(entity);

            return View(model);
        }

        public ActionResult Law()
        {
            var banner = Service.GetBanner(4);
            var model = Mapper.Map<Banner, BannerModel>(banner);
            return View(model);
        }
        public ActionResult Item(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var entity = Service.Find<Product>(id.Value);
            if (entity == null)
            {
                return HttpNotFound();
            }
            var model = Mapper.Map<Product, ProductModel>(entity);
            return View(model);
        }

        public ActionResult NewsItem(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var entity = Service.Find<News>(id.Value);
            if (entity == null)
            {
                return HttpNotFound();
            }
            var model = Mapper.Map<News, NewsModel>(entity);
            return View(model);
        }
    }
}
